<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/3/27
 * Time: 11:00
 */
namespace app\index\controller;

use app\common\model\PeopleRegisterReset;
use app\common\model\TypeMajor;
use think\Cache;

class PeopleRegister extends IndexBase
{
    private $condition = [
        /*'register_type'=>"注册土木工程师（岩土）,注册土木工程师（岩土）,二级注册建筑师,二级注册建筑师",
        'register_major'=>"矿山工程,化工石油工程,冶炼工程,化工石油工程",*/
        'register_type'=>"注册公用设备工程师（给水排水）,注册化工工程师",
        'register_major'=>","
    ];

    protected  $peopleRegisterReset ;

    public function __construct()
    {
        $this->peopleRegisterReset = new PeopleRegisterReset();
        parent::__construct();
    }
    public function getMajor()
    {
       $type_major = TypeMajor::getIdByTypeMajor();
       $data = [] ;
       foreach ($type_major as $k=>$v){
           $res = Cache::get($v['type_major']);
           if($res){
               list($data[$k]['register_type'],$data[$k]['register_major']) = explode('--',$v['type_major']);
               $data[$k]['people_id'] = implode(',',array_column($res,'people_id'));
               $data[$k]['regiser_type_major'] = $v['type_major'];
               $data[$k]['status'] = 1;
           }else{
               echo $v['type_major']."<br>";
           }

       }


      $res1 = $this->peopleRegisterReset->insertAll($data);
      if($res1){
          echo 111;
      }else{
          echo 222;exit;
      }
    }

    public function getCount()
    {
        $res = $this->newQueryLogic();
        dump($res);exit;
    }

    #处理查询逻辑(新的)
    public function newQueryLogic()
    {

        $register_type  = explode(',',$this->condition['register_type']);

        $register_major = explode(',',$this->condition['register_major']);

        $where = $map = [];
        foreach ($register_type as $k=>$v){

            if(empty($register_major[$k])){ //收集只查询类型的条件
                $where[] = "$v";
            }else{ //收集查询类型并有专业的条件
                $map1['register_type']  = $v;
                $map1['register_major'] = $register_major[$k];
                $map[] = $map1;
            }
        }
        //开始查询
        if(!empty($where)){
            $binji = $this->peopleRegisterReset->getListDataIn($where,'id,register_type,register_major,group_concat(people_id)')->toArray();

            $bin_people_arr = array_column($binji,'group_concat(people_id)');
            foreach ($bin_people_arr as $k=>$v){
                $bin_people_arr[$k]= explode(',',$v);
                $bin_people_arr[$k] =array_filter($bin_people_arr[$k]);
            }

            $jiao_people_arr_data =  call_user_func_array ('array_intersect',$bin_people_arr);
        }

        //满足类型-专业条件查找
        $jiao = [] ;
        foreach ($map as $k=>$value){
            $jiao[] = $this->peopleRegisterReset->getListData($value,'id,register_type,register_major,people_id')->toArray()[0];
        }
        $jiao_people_str = array_column($jiao,'people_id');
        foreach ($jiao_people_str as $k=>$v){
            $jiao_people_str[$k] = explode(',',$v);
        }

        if(count($jiao_people_str)>1){
            $jiao_arr = call_user_func_array ('array_intersect', $jiao_people_str);
        }else{
            $jiao_arr = $jiao_people_str;
        }

        //对最后的结果取交集
        if(empty($jiao_arr)){
           // $bin_people_arr_data = array_unique($bin_people_arr_data);
           return $jiao_people_arr_data;
        }elseif(empty($bin_people_arr_data)){
            return $jiao_arr;
        }else{
            $bin_people_arr_data = array_unique($bin_people_arr_data);
            return array_intersect($bin_people_arr_data,$jiao_arr);
        }
    }
}